using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class MenstrualSymptomController : ControllerBase
    {
        private readonly IMenstrualSymptomService _menstrualSymptomService;

        public MenstrualSymptomController(IMenstrualSymptomService menstrualSymptomService)
        {
            _menstrualSymptomService = menstrualSymptomService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<MenstrualSymptom>> GetMenstrualSymptom(int id)
        {
            var symptom = await _menstrualSymptomService.GetMenstrualSymptomByIdAsync(id);
            if (symptom == null)
            {
                return NotFound();
            }
            return symptom;
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<IEnumerable<MenstrualSymptom>>> GetMenstrualSymptomsByUserId(int userId)
        {
            var symptoms = await _menstrualSymptomService.GetMenstrualSymptomsByUserIdAsync(userId);
            return Ok(symptoms);
        }

        [HttpGet("user/{userId}/daterange")]
        public async Task<ActionResult<IEnumerable<MenstrualSymptom>>> GetMenstrualSymptomsByDateRange(
            int userId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            var symptoms = await _menstrualSymptomService.GetMenstrualSymptomsByDateRangeAsync(userId, startDate, endDate);
            return Ok(symptoms);
        }

        [HttpGet("user/{userId}/stats")]
        public async Task<ActionResult<object>> GetMenstrualSymptomStats(int userId)
        {
            var symptomStats = await _menstrualSymptomService.GetSymptomStatisticsAsync(userId);
            return Ok(symptomStats);
        }

        [HttpPost]
        public async Task<ActionResult<MenstrualSymptom>> CreateMenstrualSymptom(MenstrualSymptom symptom)
        {
            var createdSymptom = await _menstrualSymptomService.CreateMenstrualSymptomAsync(symptom);
            return CreatedAtAction(nameof(GetMenstrualSymptom), new { id = createdSymptom.Id }, createdSymptom);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateMenstrualSymptom(int id, MenstrualSymptom symptom)
        {
            if (id != symptom.Id)
            {
                return BadRequest();
            }

            await _menstrualSymptomService.UpdateMenstrualSymptomAsync(symptom);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteMenstrualSymptom(int id)
        {
            await _menstrualSymptomService.DeleteMenstrualSymptomAsync(id);
            return NoContent();
        }
    }
}